package com.zlyx.easy.core.utils;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Currency;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.springframework.web.multipart.MultipartFile;

import com.zlyx.easy.core.list.Lists;
import com.zlyx.easy.core.map.EasyMap;

public class TypeUtils {

	private static final Set<String> baseTypes = Lists.newHashSet("int", "date", "string", "double", "float", "boolean",
			"byte", "object", "long", "date-time", "__file", "biginteger", "bigdecimal", "uuid");

	private static final Map<Type, String> typeNameLookup = EasyMap.<Type, String>newMap().addValue(Long.TYPE, "long")
			.addValue(Short.TYPE, "int").addValue(Integer.TYPE, "int").addValue(Double.TYPE, "double")
			.addValue(Float.TYPE, "float").addValue(Byte.TYPE, "byte").addValue(Boolean.TYPE, "boolean")
			.addValue(Character.TYPE, "string")

			.addValue(Date.class, "date-time").addValue(java.sql.Date.class, "date").addValue(String.class, "string")
			.addValue(Object.class, "object").addValue(Long.class, "long").addValue(Integer.class, "int")
			.addValue(Short.class, "int").addValue(Double.class, "double").addValue(Float.class, "float")
			.addValue(Boolean.class, "boolean").addValue(Byte.class, "byte").addValue(BigDecimal.class, "bigdecimal")
			.addValue(BigInteger.class, "biginteger").addValue(Currency.class, "string").addValue(UUID.class, "uuid")
			.addValue(MultipartFile.class, "__file");

	private static final Map<String, Type> typeNameLookup2 = EasyMap.<String, Type>newMap().addValue("long", Long.TYPE)
			.addValue("short", Short.TYPE).addValue("int", Integer.TYPE).addValue("double", Double.TYPE)
			.addValue("float", Float.TYPE).addValue("byte", Byte.TYPE).addValue("boolean", Boolean.TYPE)
			.addValue("string", Character.TYPE);

	public static String typeNameFor(Type type) {
		return typeNameLookup.get(type);
	}

	public static boolean isBaseType(String typeName) {
		return baseTypes.contains(typeName);
	}

	public static Class<?> getBaseType(Object typeName) {
		return (Class<?>) typeNameLookup2.get(typeName.toString().toLowerCase());
	}
}
